@tool
extends Node

## 背景管理器
## 这个脚本用于在编辑器中添加和管理视差背景层

# 设置视差背景层的参数
class LayerSettings:
	var name: String
	var texture_path: String
	var motion_scale: Vector2
	var motion_mirroring: Vector2
	
	func _init(p_name: String, p_texture_path: String, p_motion_scale: Vector2, p_motion_mirroring: Vector2):
		name = p_name
		texture_path = p_texture_path
		motion_scale = p_motion_scale
		motion_mirroring = p_motion_mirroring

# 找到指定的视差背景节点
static func find_parallax_background(scene_root: Node) -> ParallaxBackground:
	if scene_root is ParallaxBackground:
		return scene_root
		
	for child in scene_root.get_children():
		if child is ParallaxBackground:
			return child
			
		var result = find_parallax_background(child)
		if result:
			return result
			
	return null

# 在编辑器中添加一个新的背景层
static func add_background_layer(
	scene_root: Node, 
	layer_name: String, 
	texture_path: String, 
	motion_scale: Vector2 = Vector2(0.5, 0.5), 
	motion_mirroring: Vector2 = Vector2(1280, 0)
) -> bool:
	var parallax_background = find_parallax_background(scene_root)
	if not parallax_background:
		push_error("没有找到视差背景节点")
		return false
	
	# 检查同名层是否已存在
	if parallax_background.has_node(layer_name):
		push_error("已存在同名背景层：" + layer_name)
		return false
	
	# 加载纹理
	var texture = load(texture_path)
	if not texture:
		push_error("无法加载纹理：" + texture_path)
		return false
	
	# 创建新的视差层
	var layer = ParallaxLayer.new()
	layer.name = layer_name
	layer.motion_scale = motion_scale
	layer.motion_mirroring = motion_mirroring
	parallax_background.add_child(layer)
	layer.owner = scene_root.owner if scene_root.owner else scene_root
	
	# 创建精灵节点
	var sprite = Sprite2D.new()
	sprite.name = "Sprite2D"
	sprite.texture = texture
	sprite.centered = false
	layer.add_child(sprite)
	sprite.owner = scene_root.owner if scene_root.owner else scene_root
	
	print("成功添加背景层：" + layer_name)
	return true

# 在游戏运行时加载背景层
static func load_background_layers(parallax_background: ParallaxBackground, layers: Array[LayerSettings]) -> void:
	if not parallax_background:
		push_error("无效的视差背景节点")
		return
	
	for layer_setting in layers:
		var texture = load(layer_setting.texture_path)
		if not texture:
			push_error("无法加载纹理：" + layer_setting.texture_path)
			continue
		
		# 检查层是否存在
		if parallax_background.has_node(layer_setting.name):
			var layer = parallax_background.get_node(layer_setting.name)
			var sprite = layer.get_node_or_null("Sprite2D")
			
			# 更新现有层
			layer.motion_scale = layer_setting.motion_scale
			layer.motion_mirroring = layer_setting.motion_mirroring
			
			if sprite:
				sprite.texture = texture
			else:
				sprite = Sprite2D.new()
				sprite.name = "Sprite2D"
				sprite.texture = texture
				sprite.centered = false
				layer.add_child(sprite)
		else:
			# 创建新层
			var layer = ParallaxLayer.new()
			layer.name = layer_setting.name
			layer.motion_scale = layer_setting.motion_scale
			layer.motion_mirroring = layer_setting.motion_mirroring
			parallax_background.add_child(layer)
			
			var sprite = Sprite2D.new()
			sprite.name = "Sprite2D"
			sprite.texture = texture
			sprite.centered = false
			layer.add_child(sprite) 